package com.cmj.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmj.Utils.BeanCopyUtils;
import com.cmj.dao.RoleMapper;
import com.cmj.domain.ResponseResult;
import com.cmj.domain.SystemConstants;
import com.cmj.domain.dto.RoleAddDto;
import com.cmj.domain.dto.RoleDto;
import com.cmj.domain.dto.RoleStatusDto;
import com.cmj.domain.entity.Role;
import com.cmj.domain.vo.PageVo;
import com.cmj.domain.vo.RoleListVo;
import com.cmj.domain.vo.RoleUpdateVo;
import com.cmj.service.RoleMenuService;
import com.cmj.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuService roleMenuService;

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        //判断是否是管理员 如果是返回集合中只需要有admin
        if(id == 1L){
            List<String> roleKeys = new ArrayList<>();
            roleKeys.add("admin");
            return roleKeys;
        }
        //否则查询用户所具有的角色信息
        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public ResponseResult<Role> listAllRole() {
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("del_flag", SystemConstants.STATUS_NORMAL);
        roleQueryWrapper.eq("status",SystemConstants.STATUS_NORMAL);
        List<Role> roles = roleMapper.selectList(roleQueryWrapper);
        return ResponseResult.okResult(roles);
    }

    @Override
    public Long[] getAllUserRole(Long id) {
        Long[] roles = roleMapper.getAllUserRole(id);
        return roles;
    }

    @Override
    public ResponseResult getRoleList(Integer pageNum, Integer pageSize, RoleDto roleDto) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(roleDto.getRoleName()),Role::getRoleName,roleDto.getRoleName());
        wrapper.eq(StringUtils.hasText(roleDto.getStatus()),Role::getStatus,roleDto.getStatus());

        Page<Role> rolePage = new Page<>();
        rolePage.setCurrent(pageNum);
        rolePage.setSize(pageSize);
        page(rolePage,wrapper);
        List<RoleListVo> roleListVos = BeanCopyUtils.copyBeanList(rolePage.getRecords(), RoleListVo.class);
        PageVo pageVo = new PageVo(roleListVos, rolePage.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult delRole(Integer[] ids) {
        int i = roleMapper.deleteBatchIds(Arrays.asList(ids));
        if (i == 0){
            new RuntimeException(SystemConstants.DELETE_FAIL);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult changeStatus(RoleStatusDto role) {
        Role role1 = new Role();
        role1.setId(role.getRoleId());
        role1.setStatus(role.getStatus());
        int i = roleMapper.updateById(role1);
        if (i == 0){
            new RuntimeException(SystemConstants.UPDATE_FAIL);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult addRole(RoleAddDto roleAddDto) {
        Role role = BeanCopyUtils.copyBean(roleAddDto, Role.class);
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("role_name",role.getRoleName());
        roleQueryWrapper.eq("role_key",role.getRoleKey());

        //新增角色和对应菜单规则
        if (roleMapper.selectList(roleQueryWrapper).size() == 0){
            roleMapper.insert(role);
            Long id = roleMapper.selectOne(roleQueryWrapper).getId();
            roleMenuService.addRoleMenu(id,roleAddDto.getMenuIds());
        }else{//更新角色
            Long id = roleMapper.selectOne(roleQueryWrapper).getId();
            role.setId(id);
            roleMapper.updateById(role);
            roleMenuService.delRoleMenu(id);
            roleMenuService.addRoleMenu(id,roleAddDto.getMenuIds());
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getUserRole(Long id) {
        Role role = roleMapper.selectById(id);
        RoleUpdateVo roleVo = BeanCopyUtils.copyBean(role, RoleUpdateVo.class);
        return ResponseResult.okResult(roleVo);
    }

}